package com.sergio.accounting.util;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * JWT调试工具类
 * 用于分析和调试JWT token
 */
@Slf4j
@Component
public class JwtDebugUtil {

    /**
     * 分析JWT token的详细信息
     */
    public void analyzeToken(String token, String secret) {
        try {
            log.info("=== JWT Token 分析 ===");
            log.info("Token长度: {}", token.length());
            log.info("Token前50字符: {}", token.substring(0, Math.min(50, token.length())));
            
            Claims claims = Jwts.parser().setSigningKey(secret).parseClaimsJws(token).getBody();
            
            log.info("Token Claims:");
            log.info("  主题 (Subject): {}", claims.getSubject());
            log.info("  签发时间 (IssuedAt): {}", claims.getIssuedAt());
            log.info("  过期时间 (Expiration): {}", claims.getExpiration());
            log.info("  用户ID: {}", claims.get("userId"));
            log.info("  用户名: {}", claims.get("username"));
            log.info("  登录时间: {}", claims.get("loginTime"));
            log.info("  随机ID: {}", claims.get("randomId"));
            
            // 检查是否过期
            Date now = new Date();
            boolean isExpired = claims.getExpiration().before(now);
            log.info("  是否过期: {}", isExpired);
            if (isExpired) {
                log.warn("  Token已过期！过期时间: {}, 当前时间: {}", claims.getExpiration(), now);
            }
            
            // 计算剩余有效期
            long remainingTime = claims.getExpiration().getTime() - now.getTime();
            long remainingMinutes = remainingTime / (1000 * 60);
            log.info("  剩余有效期: {} 分钟", remainingMinutes);
            
        } catch (Exception e) {
            log.error("JWT token分析失败: {}", e.getMessage());
        }
    }

    /**
     * 比较两个token是否相同
     */
    public void compareTokens(String token1, String token2, String secret) {
        log.info("=== JWT Token 比较 ===");
        log.info("Token1: {}", token1.substring(0, Math.min(50, token1.length())));
        log.info("Token2: {}", token2.substring(0, Math.min(50, token2.length())));
        
        boolean isIdentical = token1.equals(token2);
        log.info("Token是否完全相同: {}", isIdentical);
        
        if (isIdentical) {
            log.warn("⚠️  警告：两个token完全相同！这通常表明有问题。");
        } else {
            log.info("✅ Token不同，这是正常的。");
        }
        
        // 分析两个token
        log.info("分析Token1:");
        analyzeToken(token1, secret);
        
        log.info("分析Token2:");
        analyzeToken(token2, secret);
    }

    /**
     * 生成测试token信息
     */
    public void generateTestTokenInfo() {
        log.info("=== 测试JWT Token生成 ===");
        long currentTime = System.currentTimeMillis();
        String randomId = java.util.UUID.randomUUID().toString();
        
        log.info("当前时间戳: {}", currentTime);
        log.info("随机UUID: {}", randomId);
        log.info("时间差: {} ms", System.currentTimeMillis() - currentTime);
    }
}
